package com.fare.taluo.action.system.version;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.application.system.version.VersionInfoApplication;
import com.fare.taluo.base.struts2.json.BaseJsonStrutsAction;
import com.fare.taluo.base.struts2.json.JsonReturnBean;
import com.fare.taluo.util.param.ParamIntegerUtils;
import com.fare.taluo.util.param.ParamUtils;
import com.fare.taluo.vo.core.VersionDetailInfoVO;
import com.fare.taluo.vo.core.VersionInfoVO;

@Controller("versionInfoAction")
@Namespace("/versioninfo")
@Scope("prototype")
public class VersionInfoAction extends BaseJsonStrutsAction {
	private static final long serialVersionUID = -7218105703853722057L;
	@Resource(name = "versionInfoApplication")
	private VersionInfoApplication versionInfoApplication;

	private List<VersionInfoVO> versionInfoVOs;

	private Map<String, Object> versionInfoMap;

	public Map<String, Object> getVersionInfoMap() {
		return versionInfoMap;
	}

	public List<VersionInfoVO> getVersionInfoVOs() {
		return versionInfoVOs;
	}

	private String ids;// 批量删除

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	

	public VersionInfoVO getVersionInfoVO() {
		return versionInfoVO;
	}

	public void setVersionInfoVO(VersionInfoVO versionInfoVO) {
		this.versionInfoVO = versionInfoVO;
	}

	private VersionInfoVO versionInfoVO;

	// 首页获取版本信息
	@Action(value = "show", results = {@Result(name = "success", type="json", params={"root","versionInfoVO"}) })
	public String show() {
		versionInfoVO = versionInfoApplication.getMaxVersionInfo();
		return SUCCESS;
	}
	// 首页获取版本信息
	@Action(value = "showAll", results = {@Result(name = "success", type="json", params={"root","versionInfoVOs"}) })
	public String showAll() {
		versionInfoVOs = versionInfoApplication.findversionEntities();
		return SUCCESS;
	}
	
	// 首页获取版本详细信息
	@Action(value = "detail", results = {@Result(name = "success", type="json", params={"root","versionInfoVO"}) })
	public String detail() {
		Integer version_Id = ParamIntegerUtils.getParameter(request, "version_Id", 1);
		versionInfoVO = versionInfoApplication.findVersionInfoVO(new Long(version_Id));
		return SUCCESS;
	}
	
	@Action(value = "list", results = {@Result(name = "success", type="dispatcher", location="/pages/system/version/versionInfo_list.jsp") })
	public String list() {
		return SUCCESS;
	}
	@Action(value = "all", results = {@Result(name = "success", type="dispatcher", location="/pages/system/version/versionInfo_all.jsp") })
	public String all() {
		return SUCCESS;
	}
	@Action(value = "publish", results = {@Result(name = "success", type="dispatcher", location="/pages/system/version/versionInfo_publish.jsp") })
	public String publish() {
		return SUCCESS;
	}
	// 管理页面加载数据
	@Action(value = "listJSON", results = {@Result(name = "success", type="json", params={"root","versionInfoMap"}) })
	public String listJSON() throws Exception {
		versionInfoVOs = versionInfoApplication.findversionEntities();
		versionInfoMap = new HashMap<String, Object>();
		versionInfoMap.put("Rows", versionInfoVOs);
		return SUCCESS;
	}
	@Action(value = "save", results = {@Result(name = "success", type="json", params={"root","returnBean"}) })
	public String save() throws Exception {
		List<VersionDetailInfoVO> details = null;
		try{
			details = new ArrayList<VersionDetailInfoVO>();
			Integer[] seqs = ParamIntegerUtils.getParameters(request, "seq_Num", 1);
			String[] contents = ParamUtils.getParameters(request, "content","");
			for(int i = 0 ; i < seqs.length; i++){
				details.add(new VersionDetailInfoVO(seqs[i], contents[i]));
			}
			returnBean = new JsonReturnBean();
			versionInfoVO.setVersionDetailInfoVOs(details);
			versionInfoApplication.saveOrUpdate(versionInfoVO);
			returnBean.setHasOk(true);
			returnBean.setTip(JsonReturnBean.TIP_SUCCESS);
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e);
			returnBean.setHasOk(false);
			returnBean.setTip(JsonReturnBean.TIP_FAIL);
			returnBean.setMessage(e.getMessage());
		}
		return SUCCESS;
	}
	@Action(value = "delete", results = {@Result(name = "success", type="json", params={"root","returnBean"}) })
	public String delete() {
		try{
			returnBean = new JsonReturnBean();
			String[] id = ids.split(",");
			for (int i = 0; i < id.length; i++) {
				versionInfoApplication.delete(Long.parseLong(id[i]));
			}
			returnBean.setHasOk(true);
			returnBean.setTip(JsonReturnBean.TIP_SUCCESS);
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e);
			returnBean.setHasOk(false);
			returnBean.setTip(JsonReturnBean.TIP_FAIL);
			returnBean.setMessage(e.getMessage());
		}
		return SUCCESS;
	}

}
